//
//  PayMoneyController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/8/23.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "PayMoneyController.h"
#import "OrderPaymentCell.h"
#import "PayMoneyTextCell.h"

@interface PayMoneyController ()<UITextFieldDelegate>
{
    UITextField *_moneyField;
    UIView *_footView;
}

@property(nonatomic,strong)NSMutableArray *boolArray;

@property (nonatomic,strong)NSDictionary *userInfoDict;

@end

@implementation PayMoneyController

- (NSMutableArray *)boolArray
{
    if (!_boolArray) {
        _boolArray = [NSMutableArray arrayWithObjects:@"0",@"1",@"0", nil];
    }
    return _boolArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [self loadUserInfoNumberData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.title = @"充值";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PayMoneyTextCell" bundle:nil] forCellReuseIdentifier:@"PayMoneyTextCell"];
    self.tableView.scrollEnabled = NO;

//    [self payViewSetupUI];
}


#pragma mark>>>>>>>>DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 1;
    }else{
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier= @"payCell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    NSDictionary *dataDict = self.userInfoDict[@"data"];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = FONT(15);
        cell.detailTextLabel.font = FONT(15);
    }
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"充值账户";
            cell.detailTextLabel.text = USERNAME;
            return cell;
        }else{
            cell.textLabel.text = @"账户余额";
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@元",dataDict[@"yue"]];
            if (self.userInfoDict == nil) {
                cell.detailTextLabel.text = @"";
            }
            
            return cell;
        }
        
    }else if (indexPath.section ==1){
        
        PayMoneyTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PayMoneyTextCell" forIndexPath:indexPath];
        _moneyField = cell.PayTextField;
        cell.PayTextField.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.PayTextField.keyboardType = UIKeyboardTypeNumberPad;
        
        return cell;

    }else{
        
        if (indexPath.row == 0) {
            cell.textLabel.text = @"支付方式";
            return cell;
        }else{
            
            static NSString *OrderPaymentCellID= @"paySdkCell";
            
            OrderPaymentCell *cell=[tableView dequeueReusableCellWithIdentifier:OrderPaymentCellID];
            
            if (cell==nil) {
                cell= [[[NSBundle mainBundle] loadNibNamed:@"OrderPaymentCell" owner:nil options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
            }
            
            if ([self.boolArray[indexPath.row] isEqualToString:@"1"]) {
                cell.selectImg.image = [UIImage imageNamed:@"shoujichongzhi_0"];
            } else {
                cell.selectImg.image = [UIImage imageNamed:@"shoujichongzhi_1"];
            }
            cell.selectIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"dingdanzhifu_%ld",indexPath.row+2]];
            
            NSArray *arr = @[@"",@"微信",@"支付宝"];
            cell.payTittle.text = [NSString stringWithFormat:@"%@",arr[indexPath.row]];
            return cell;
        }
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [_moneyField resignFirstResponder];
    NSLog(@"%zd",indexPath.row);
    
    if (indexPath.section == 2 && indexPath.row > 0) {
        for (NSInteger i=0; i<self.boolArray.count; i++) {
            if (indexPath.row == i) {
                self.boolArray[i] = @"1";
            }else{
                self.boolArray[i] = @"0";
            }
        }
    }
    
    [self.tableView reloadData];
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section ==0 || section == 1) {
        return 0.01;
    }else{
        return HEIGHT/10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if (section == 0 || section == 1) {
        return nil;
    }else{
        
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/10)];
        UIButton *rechargeBtn = [[UIButton alloc] init];
        [rechargeBtn setTitle:@"立即充值" forState:UIControlStateNormal];
        [_footView addSubview:rechargeBtn];
        rechargeBtn.layer.cornerRadius = 5;
        rechargeBtn.layer.masksToBounds = YES;
        rechargeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [rechargeBtn setBackgroundColor:DEFAULT_COLOR];
        
        [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(WIDTH-30);
            make.centerY.equalTo(_footView.mas_centerY).offset(35/2);
            make.left.equalTo(_footView.mas_left).offset(15);
            make.height.mas_equalTo(35);
        }];
        
        [rechargeBtn addTarget:self action:@selector(rechargeBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        return _footView;
    }
}

- (void)rechargeBtnDidClick
{
    [_moneyField resignFirstResponder];
    
    if (![_moneyField.text length]) {
        //弹框
        MBProgressHUD *hud = [MBProgressHUD showMessage:@"请输入金额" toView:self.view dimBackground:NO AndCompleteBlock:nil];
        [hud show:YES];
    }
    
    NSLog(@"%@",_moneyField.text);
    
}

- (void)loadUserInfoNumberData {
    
    NSDictionary *params = @{
                             @"username":USERNAME
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"AppUser/UserInfo" completeBlock:^(id object) {
        self.userInfoDict = object;
        [self.tableView reloadData];
    }];
}


@end
